package com.hh.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hh.commonutils.Result;
import com.hh.eduservice.entity.EduCourse;
import com.hh.eduservice.entity.EduTeacher;
import com.hh.eduservice.mapper.EduTeacherMapper;
import com.hh.eduservice.service.EduCourseService;
import com.hh.eduservice.service.EduTeacherService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 讲师 服务实现类
 * </p>
 *
 * @author huhuan
 * @since 2021-09-04
 */
@Service
public class EduTeacherServiceImpl extends ServiceImpl<EduTeacherMapper, EduTeacher> implements EduTeacherService {
    @Autowired
    private EduTeacherMapper eduTeacherMapper;
    @Autowired
    private EduCourseService courseService;

    @Override
    public ArrayList getLimitEduTeacher() {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.orderByDesc("id");
        queryWrapper.last("limit 4");
        ArrayList<EduTeacher> list = (ArrayList<EduTeacher>) eduTeacherMapper.selectList(queryWrapper);
        return list;
    }

    @Override
    public HashMap<String, Object> getLimitEduTeachers(Page pagePram) {
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("id");
        eduTeacherMapper.selectPage(pagePram, wrapper);

        long current = pagePram.getCurrent();
        long pages = pagePram.getPages();
        long size = pagePram.getSize();
        long total = pagePram.getTotal();
        boolean hasNext = pagePram.hasNext();
        boolean hasPrevious = pagePram.hasPrevious();
        List<EduTeacher> records = pagePram.getRecords();

        HashMap<String, Object> map = new HashMap<>();
        map.put("current", current);
        map.put("pages", pages);
        map.put("size", size);
        map.put("total", total);
        map.put("items", records);
        map.put("hasNext", hasNext);
        map.put("hasPrevious", hasPrevious);
        return map;
    }

    @Override
    public Result getTeacherInfoAndCourses(String id) {
        EduTeacher eduTeacher = eduTeacherMapper.selectById(id);
        List<EduCourse> eduCourseArrayList = courseService.getCourseByTeacherId(id);
        if (eduTeacher != null && eduCourseArrayList.size() != 0) {
            return Result.success().data("teacher",eduTeacher).data("courseList",eduCourseArrayList);
        }
        return Result.success().data("teacher", eduTeacher).data("courseList", new ArrayList<>());
    }


}
